<?php

ini_set('date.timezone','Asia/Shanghai');

require_once 'sql.php';

open_logger();

$conf = array(
	'ip' => '127.0.0.1',
	'port' => 3306,
	'user' => 'wine',
	'pass' => 'drink_wine',
	'dbname' => 'wine',
);

$mysql = sql_connect($conf);
if (!$mysql) {
	logger("connect database failed");
	exit(-1);
}

function new_member($member) {
	$standard = array(
		'name' => '',
		'phone' => '',
		'sex' => 0,
		'birthday' => '',
	);

	if (count(array_diff_key($member, $standard)) != 0) {
		logger("new member property is not valid");
		return false;
	}

	if (!is_numeric($member['phone']) || !is_numeric($member['sex'])) {
		logger("new member property is not valid");
		return false;
	}

	$sql = sprintf("insert into member(name, phone, sex, birthday, discount, consume, gmt_create, gmt_modified) values('%s', '%d', %d, %s, 100, 0, NOW(), NOW())",
			$member['name'],$member['phone'],$member['birthday'],$member['sex']);

	return sql_insert($sql) != 0;
}

function query_member_by_name($name) {
	$sql = sprintf("select * from member where name='%s'", $name);
	return sql_query($sql);
}

function query_member_by_phone($phone) {
	$sql = sprintf("select * from member where phone='%s'", $phone);
	return sql_query($sql);
}

function query_member_with_like($field) {
	$sql = "select * from member";
	if (strlen($field) != 0) 
		$sql = $sql . " where name like '%$field%' or phone like '%$field%' or birthday like '%$field%'";
	return sql_query($sql);
}

function __update_member_info($phone, $updated) {
	if (count($updated) == 0)
		return false;

	$sql = "update member set ";

	if (array_key_exists("discount", $updated))
		$sql = $sql . "discount=" . $updated["discount"] . ",";
	if (array_key_exists("consume", $updated))
		$sql = $sql . ("consume=" . $updated["consume"] . ",");
	if (array_key_exists("coupon", $updated))
		$sql = $sql . ("coupon=" . $updated["coupon"] . ",");

	$sql = $sql . ("gmt_modified=NOW() where phone='". $phone. "'");

	return sql_update($sql);
}

function update_member_discount($phone, $discount) {
	return __update_member_info($phone, array("discount"=>$discount));
}

function update_member_consume_money($phone, $consume) {
	return __update_member_info($phone, array("consume"=>$consume));
}


function update_member_coupon($phone, $coupon) {
	$sql = "update member set gmt_modified=NOW(), coupon=". $coupon. " where phone='$phone'";
	return sql_update($sql);
}

function increase_member_consume_money($phone, $consume) {
	$sql = "update member set gmt_modified=NOW(), consume=consume+". $consume . " where phone='$phone'";
	return sql_update($sql);
}

function get_all_goods() {
	return sql_query("select * from goods");
}

function buy($info) {
	$standard = array(
		'phone' => '',
		'goods' => '',
		'goods_count' => 0,
		'money' => 0,
		//'referrals' => '',
	);

	if (count(array_diff_key($info, $standard)) != 0) {
		logger("buy action property is not valid");
		return false;
	}

	$custom = query_member_by_phone($info['phone']);

	if (count($custom) == 0) {
		logger("phone not exist");
		return false;
	}

//	// gift coupon
//	if (array_key_exists('referrals', $info)) {
//		if (count(query_member_by_phone($standard['referrals'])) == 0) {
//			logger("referrals not exist");
//			return false;
//		}
//		increase_member_coupon($info['referrals'], (float($info['money']) * 0.03)
//	}

	// insert new consume record
	if (1 != sql_insert("insert into purchase(name, phone, goods, goods_count, money, gmt_create, gmt_modified) values('" . $custom[0]['name'] . "', '" . $info['phone'] . "','" . $info["goods"]. "', " . $info["goods_count"] . ", ". $info["money"] . ", NOW(), NOW())")) {
		logger("insert purchase failed");
		return false;
	}
	error_log('updated', 0);
	// update total consume
	increase_member_consume_money($info['phone'], $info["money"]);
	
	return true;
}

?>
